const app = getApp();

import bData from '../../service/mock.js'
import util, {
    vxios,
    baseURL
} from '../../utils/util.js';

Page({
    data: {
        contentHeight: app.globalData.windowHeight,
        TabCur: null,
        itemLeftSize: 0,
        itemRightSize: 0,
        themes: [],
        items: [],
        pigeSize: 6,
        pageNum: 1,
        AllPage: 0,
        test: 'test'
    },
    // 页面加载
    onLoad: function (options) {
        // this.GetArticles();
        // this.GetAllPageNum();
        this.GetUserInfo();

        var i = bData.getData(this.data.pageNum);
        // console.log(i)
        var arr = i.items;
        var len = arr.length;
        var itemLeftSize = 0,
            itemRightSize = 0;
        for (let i = 0; i < len; i++) {
            if (i % 2 == 0) {
                arr[i].columnPosition = 'left';
                itemLeftSize++;
            } else {
                arr[i].columnPosition = 'right';
                itemRightSize++;
            }
        }
        this.setData({
            TabCur: i.themes[0].id,
            themes: i.themes,
            items: arr,
            itemLeftSize: itemLeftSize,
            itemRightSize: itemRightSize
        });
        console.log(this.data.items);
        // this.GetArticles(0)

    },


    // 拉触底加载
    onReachBottom: function () {
        console.log('触底');
        this.GetArticles(0)
        // masonry.method.ap
        // this.selectComponent('')
    },

    // tab选择事件
    tabSelect(e) {
        var id = e.currentTarget.dataset.id;
        // var data = bData.getData(1, id);
        this.setData({
            TabCur: id,
            items: this.data.items.slice(-8)
            // items: data.items,
            // pageNum: 1
        })
        this.GetArticles(0)
        //...
    },

    GetArticles(no) {
        var than = this;
        wx.request({
            // url: baseURL+'articles',
            url: 'https://mock.apipost.net/mock/4077a55ac0e0000/?apipost_id=4df31a5f58007',
            method: 'GET',
            success: function (res) {
                var arr = than.data.items;
                var itemLeftSize = than.data.itemLeftSize;
                var itemRightSize = than.data.itemRightSize;
                for (let i = 0; i < res.data.length; i++) {
                    if (itemLeftSize <= itemRightSize) {
                        res.data[i].columnPosition = 'left';
                        itemLeftSize += res.data[i].pictures.length > 0 ? 1 : 0;
                    } else {
                        res.data[i].columnPosition = 'right';
                        itemRightSize += res.data[i].pictures.length > 0 ? 1 : 0;
                    }
                    arr.push(res.data[i])
                }
                console.log(arr);
                than.setData({
                    items: arr,
                    itemLeftSize: itemLeftSize,
                    itemRightSize: itemRightSize
                })
            },
            fail: function (res) {
                console.error(res);
            }
        })
    },

    GetUserInfo() {
        wx.request({
          url: 'https://mock.apipost.net/mock/4077a55ac0e0000/?apipost_id=ab6c961358003',
          method: 'GET',
          success: (res) => {
            app.globalData.user = res.data
            console.log(app.globalData.user);
          }
        })
    },

    GetAllPageNum() {
        wx.request({
            url: 'http://127.0.0.1:8080/GetArticlesPageCounts',
            method: 'GET',
            success: (res) => {
                this.setData({
                    AllPage: res.data
                })
            }
        })
    }
})